package com.devunion.reader.client.panel;

import com.devunion.reader.client.core.AbstractComposite;
import com.devunion.reader.client.to.SearchProjectTo;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.binding.Bindings;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.SimpleComboBoxFieldBinding;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.*;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;

/**
 * @author Timoshenko Alexander 22.08.2009 : 14:33:44
 */
public class ExpertSearchPanel extends SimpleSearchPanel {
    private TextField projectName = new TextField();
    private TextField title = new TextField();
    private SimpleComboBox<String> type = new SimpleComboBox<String>();
    private TextField language = new TextField();
    private NumberField pagesMin = new NumberField();
    private NumberField pagesMax = new NumberField();
    private DateField creationMin = new DateField();
    private DateField creationMax = new DateField();
    private DateField exactMin = new DateField();
    private DateField exactMax = new DateField();
    private DateField endMin = new DateField();
    private DateField endMax = new DateField();


    public void initialize() {
        FieldSet projectFieldSet = new FieldSet();
        projectFieldSet.setLayout(new ColumnLayout());
        projectFieldSet.setHeading("Project");

        LayoutContainer leftContainer = new LayoutContainer(new FormLayout(FormPanel.LabelAlign.TOP));
        projectName.setFieldLabel("Project Name");
        projectName.setEmptyText("Enter project name");
        leftContainer.add(projectName);
        title.setFieldLabel("Title");
        title.setEmptyText("Enter book title");
        leftContainer.add(title);
        type.setFieldLabel("Type");
        type.add("BOOK");
        type.add("ARTICLE");
        type.add("DOCUMENT");
        type.add("LETTER");
        type.add("OTHER");
        type.setEmptyText("Select book type");
        leftContainer.add(type);
        language.setFieldLabel("Language");
        language.setEmptyText("Enter book language");
        leftContainer.add(language);
        projectFieldSet.add(leftContainer, new ColumnData(.5));

        LayoutContainer rightContainer = new LayoutContainer(new FormLayout(FormPanel.LabelAlign.TOP));
        MultiField<NumberField> pages = new MultiField<NumberField>();
        pages.setOrientation(Style.Orientation.VERTICAL);
        pages.setStyleAttribute("overflow", "hidden");
        pages.setFieldLabel("Number of pages");
        pagesMin.setAllowDecimals(false);
        pagesMin.setAllowNegative(false);
        pagesMin.setEmptyText("Min. number of pages");
        pages.add(pagesMin);
        pagesMax.setEmptyText("Max. number of pages");
        pagesMax.setAllowDecimals(false);
        pagesMax.setAllowNegative(false);
        pages.add(pagesMax);
        rightContainer.add(pages);
        MultiField<DateField> creationDate = new MultiField<DateField>();
        creationDate.setOrientation(Style.Orientation.VERTICAL);
        creationDate.setStyleAttribute("overflow", "hidden");
        creationDate.setFieldLabel("Start Date");
        creationMin.setEmptyText("From:");
        creationDate.add(creationMin);
        creationMax.setEmptyText("To:");
        creationDate.add(creationMax);
        rightContainer.add(creationDate);
        MultiField<DateField> exactDate = new MultiField<DateField>();
        exactDate.setOrientation(Style.Orientation.VERTICAL);
        exactDate.setStyleAttribute("overflow", "hidden");
        exactDate.setFieldLabel("Exact Date");
        exactMin.setEmptyText("From:");
        exactDate.add(exactMin);
        exactMax.setEmptyText("To:");
        exactDate.add(exactMax);
        rightContainer.add(exactDate);
        MultiField<DateField> endDate = new MultiField<DateField>();
        endDate.setOrientation(Style.Orientation.VERTICAL);
        endDate.setStyleAttribute("overflow", "hidden");
        endDate.setFieldLabel("End Date");
        endMin.setEmptyText("From:");
        endDate.add(endMin);
        endMax.setEmptyText("To:");
        endDate.add(endMax);
        rightContainer.add(endDate);
        projectFieldSet.add(rightContainer, new ColumnData(.5));
        add(projectFieldSet, new ColumnData(.56));

        super.initialize();
    }

    public void bind(SearchProjectTo form) {
        super.bind(form);
        bindings.addFieldBinding(new FieldBinding(projectName, SearchProjectTo.PROJECT_NAME));
        bindings.addFieldBinding(new FieldBinding(title, SearchProjectTo.TITLE));
        bindings.addFieldBinding(new SimpleComboBoxFieldBinding(type, SearchProjectTo.TYPE));
        bindings.addFieldBinding(new FieldBinding(language, SearchProjectTo.LANGUAGE));
        bindings.addFieldBinding(new FieldBinding(pagesMin, SearchProjectTo.PAGES_MIN));
        bindings.addFieldBinding(new FieldBinding(pagesMax, SearchProjectTo.PAGES_MAX));
        bindings.addFieldBinding(new FieldBinding(creationMin, SearchProjectTo.CREATION_MIN));
        bindings.addFieldBinding(new FieldBinding(creationMax, SearchProjectTo.CREATION_MAX));
        bindings.addFieldBinding(new FieldBinding(exactMin, SearchProjectTo.EXACT_MIN));
        bindings.addFieldBinding(new FieldBinding(exactMax, SearchProjectTo.EXACT_MAX));
        bindings.addFieldBinding(new FieldBinding(endMin, SearchProjectTo.END_MIN));
        bindings.addFieldBinding(new FieldBinding(endMax, SearchProjectTo.END_MAX));

    }

    public void reset() {
        projectName.reset();
        title.reset();
        type.reset();
        language.reset();
        pagesMin.reset();
        pagesMax.reset();
        creationMin.reset();
        creationMax.reset();
        exactMin.reset();
        exactMax.reset();
        endMin.reset();
        endMax.reset();
    }
}